////////////////////////////////////////////////////////////
// Workfile : AddressReader.cpp
// Author : Andreas Lehner
// Date : 5.11.2012
// Description : read information of an Address IMPLEMENTATION
// Remarks : 0
// Revision : 1
////////////////////////////////////////////////////////////
#include "AddressReader.h"
#include "Address.h"
#include <string>
#include <vector>

using namespace std;

//read an address
void AddressReader::DoRead(string const& line, IRead *read){
	//variables for an Address
	string streetname;
	string town;
	size_t housenumber, postcode;
	//iterator on beginning of a string
	string::const_iterator start = line.begin();
	//read members of an Address
	if (start == line.end()){
		throw "AddressReader: street name expected...";
	}
	streetname = readWord(start, line.end());
	if (start == line.end()){
		throw "AddressReader: house number expected...";
	}
	housenumber = readNumber(start, line.end());
	if (start == line.end()){
		throw "AddressReader: postcode expected...";
	}
	postcode = readNumber(start, line.end());
	if (start == line.end()){
		throw "AddressReader: town expected...";
	}
	town = readWord(start, line.end());
	//a town can exist of two words
	if (start != line.end()){
		town.append(" ");
		town.append(readWord(start, line.end()));
	}
	//save information in Address
	Address adr(streetname, housenumber, postcode, town);
	//add Address to mAddresses of AddressManager
	read->addAddress(adr);
}